import cv2
import numpy as np

if __name__ == "__main__":
    # 替换以下 URL 为你的 RTSP 流地址
    # 大华
    # rtsp_url_1 = "rtsp://admin:admin123456@192.168.20.156:554/cam/realmonitor?channel=1&subtype=0"
    # rtsp_url_2 = "rtsp://admin:admin123456@192.168.20.157:554/cam/realmonitor?channel=1&subtype=0"
    # 海康
    rtsp_url_1 = "rtsp://admin:jk123456@192.168.1.64:554/h264/ch1/main/av_stream"
    rtsp_url_2 = "rtsp://admin:jk123456@192.168.1.65:554/h264/ch1/main/av_stream"
    cap_1 = cv2.VideoCapture(rtsp_url_1)
    cap_2 = cv2.VideoCapture(rtsp_url_2)

    while cap_1.isOpened() and cap_2.isOpened():
        print("111")
        # 读取摄像头的每一帧
        ret_1, frame_1 = cap_1.read()
        ret_2, frame_2 = cap_2.read()

        frame_1 = cv2.resize(frame_1, (1120, 630))
        frame_2 = cv2.resize(frame_2, (1120, 630))

        if not ret_1 or not ret_2:
            print("Failed to grab frame")
            break
        # 合并摄像头帧
        combined_frame = np.hstack((frame_1, frame_2))
        cv2.imshow('IP Camera stream', combined_frame)

        # 按 'q' 键退出循环
        if cv2.waitKey(1) & 0xFF == ord('q'):
            break

    cap_1.release()
    cap_2.release()
    cv2.destroyAllWindows()
